package iosr11.ui.data;

import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.EventService;
import iosr11.Service.ProjectGroupService;
import iosr11.Service.StudentService;
import iosr11.Service.NoteService;
import iosr11.data.ProjectGroup;
import iosr11.data.Event;
import iosr11.data.Student;
import iosr11.data.Note;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

public class GroupBean {
	private EventService event_service;
	private StudentService student_service;
	private ProjectGroupService group_service;
	private NoteService note_service;
	private Logger logger = Logger.getLogger("iosr.ui.data");
	private Event event;
	private ProjectGroup group;
	private Note note;
	private List<Note> noteList;
	private List<Student> studentList;
	private HtmlDataTable dataTable, dataTable2;
	
	public GroupBean(){
		this.event_service = (EventService)ServiceLocatorImpl.getInstance().getService(Event.class);
		if(this.event_service == null){
			logger.severe("Could not obtain reference to EventService");
			return;
		}
		Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		if(sessionMap.containsKey(SessionMapKeys.SelectedEventID.name())){	
			this.event = event_service.getEvent((String)sessionMap.get(SessionMapKeys.SelectedEventID.name()));	
			if(this.event==null){
				logger.severe("Could not obtain event from EventService");
			}
		}	
		this.group_service = (ProjectGroupService)ServiceLocatorImpl.getInstance().getService(ProjectGroup.class);
		if(this.group_service == null){
			logger.severe("Could not obtain reference to ProjectGroupService");
			return;
		}
		if(sessionMap.containsKey(SessionMapKeys.SelectedGroupName.name())){	
			this.group = group_service.getProjectGroup((String)sessionMap.get(SessionMapKeys.SelectedGroupName.name()));
			if(this.group==null){
				logger.severe("Could not obtain group from ProjectGroupService");
			}
		}	
	}
	
	public HtmlDataTable getDataTable(){
		return this.dataTable;
	}
	public void setDataTable(HtmlDataTable table){
		this.dataTable = table;
	}
	
	public HtmlDataTable getDataTable2(){
		return this.dataTable2;
	}
	public void setDataTable2(HtmlDataTable table){
		this.dataTable2 = table;
	}
	
	/*
	public boolean getEditMode(){
		return (this.event.getId()!=null);
	}
	 */
	
	public ProjectGroup getGroup() {
		return this.group;
	}
			
	public Event getEvent() {
		return this.event;
	}
	
	public List<Student> getStudents(){
		if(studentList==null){
			this.student_service = (StudentService)ServiceLocatorImpl.getInstance().getService(Student.class);
			if(this.group_service == null){
				logger.severe("Could not obtain reference to StudentService");
				return null;
			}
			studentList = student_service.getStudentsForProjectGroup(this.group);
		}
		if(studentList==null){
			logger.severe("Could not obtain student from studentService");
			return null;
		}
		return studentList;
	}
	
	public List<Note> getNotes(){
		if(noteList==null){
			this.note_service = (NoteService)ServiceLocatorImpl.getInstance().getService(Note.class);
			if(this.note_service == null){
				logger.severe("Could not obtain reference to NoteService");
				return null;
			}
			noteList = note_service.getGroupNotes(this.group,this.event.getSeries());
		}
		if(noteList==null){
			logger.severe("Could not obtain note from NoteService");
			return null;
		}
		return noteList;
	}
	/*
	public void setNotes(List<Note> noteList) {
		this.noteList = noteList;		
	}
	*/
	public Note getNote(){
		List<Note> notes;
		if(this.note_service == null)
			this.note_service = (NoteService)ServiceLocatorImpl.getInstance().getService(Note.class);
		if(this.note_service == null){
			logger.severe("Could not obtain reference to NoteService");
			return null;
		}
		notes = note_service.getGroupNotes(this.group,this.event);
		if(notes==null){
			this.note = new Note();
			this.note.setGroup(this.group);
			this.note.setEvent(this.event);
		} else
			this.note = notes.get(0); // First Note
		return this.note;
	}
	
	public void setNote(Note note){
		this.note.setText(note.getText());
	}
	
	public String updateNote(){
		if(this.note_service == null)
			this.note_service = (NoteService)ServiceLocatorImpl.getInstance().getService(Note.class);
		if(this.note_service == null){
			logger.severe("Could not obtain reference to NoteService");
			return "failure";
		}
		if(this.note_service.updateNote(this.note)){
			return "success";
		}else{
			return "failure";
		}
	}
	
	public String goToStudentMarks() {
		Student selectedStudent = (Student) dataTable2.getRowData();
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
				 SessionMapKeys.SelectedStudentEmail.name(), selectedStudent.getEmail());
		return "goToStudentMarks";
	}
}
